<?php

namespace App\Http\Controllers\admin;

use DB;
use Storage;
use Illuminate\Http\Request;
use App\Http\Controllers\common\PublicController;
//模型
use App\Models\Models;
use App\Models\Supplier;
use App\Models\Type;

class ModelController extends PublicController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        $info = DB::table('model as m')
            -> leftJoin('supplier as s','m.supplier_id','=','s.id')
            -> leftJoin('type as t','m.type_id','=','t.id')
            -> select('m.*','s.name as supplier_name','t.name as type_name')
            -> paginate(10);
        return view('admin.model.index',['data'=>$info,'request'=>$request -> all()]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getCreate()
    {
        //查询供应商表
        $supplierInfo = Supplier::select('id','name') -> get();
        //查询设备类型表
        $typeInfo = Type::select('id','name') -> get();
        return view('admin.model.create',['supplierInfo'=>$supplierInfo,'typeInfo'=>$typeInfo]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postStore(Request $request)
    {
        $info = $request -> except('_token','rule');
        $rule = $request->input('rule');
        $name = $request->input('name');

        if(empty($rule)){
                $tocont = false;
        }else{
            $tocont = Storage::put(strtolower($name).'.txt',$rule);
            $info['parse'] = strtolower($name).'.txt';
        }

        if($info['supplier_id'] != 0){
            //添加区域表
            $res = Models::create($info);
        }else{
            $res = false;
        }
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getEdit(Request $request)
    {
        $id = $request -> input('id');
        $data = Models::find($id) -> toarray();
        if(Storage::has(strtolower($data['name']).'.txt')){
        $data['rule'] = Storage::get(strtolower($data['name']).'.txt');
        }else{
            $data['rule'] = '';
        };
        //查询供应商表
        $supplierInfo = Supplier::select('id','name') -> get();
        //查询设备类型表
        $typeInfo = Type::select('id','name') -> get();
        return view('admin.model.edit',['data'=>$data,'supplierInfo'=>$supplierInfo,'typeInfo'=>$typeInfo]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postUpdate(Request $request)
    {
        $rule = $request->input('rule');
        $name = $request->input('name');
        $id = $request -> input('id');
        $info = $request -> except('id','_token','rule');
        if(empty($rule)){
            if(Storage::has(strtolower($name).'.txt')){
                $tocont = Storage::delete(strtolower($name).'.txt');
            }else{
                $tocont = false;
            }
        }else{
            $tocont = Storage::put(strtolower($name).'.txt',$rule);
             $info['parse'] = strtolower($name).'.txt';
        }
        if($info['supplier_id'] != 0){
        $res = Models::find($id) -> update($info);  
        }else{
            $res = false;
        }
        if($res||$tocont){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postDestroy(Request $request)
    {
        $id = $request -> input('id');
        $type_id = Models::find($id) -> type_id;
        $typeDB = $this -> typeDB($type_id);
        //查询区域下是否有建筑,有不能删除
        $count = DB::table($typeDB) -> where('model_id',$id) -> count();
        if($count == 0){
            //查询区域和客户的关系表并删除
            $res = Models::destroy($id);
            if($res){
                $data['status'] = 1;
            }else{
                $data['status'] = -1;
            }
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }
}
